package com.wolfheader.wolfgame.entity.admin;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wolfheader.wolfgame.entity.BaseEntity;

@Entity
@Table(name = "t_role")
public class RoleEntity extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3833810344937173003L;

	private String rolename;

	/** 权限 */
	private Set<PermissionEntity> permissions = new HashSet<PermissionEntity>();

	/** 管理员 */
	private Set<AdminEntity> admins = new HashSet<AdminEntity>();

	private String remark;

	private boolean enabled;

	// 省略 get set 方法

	public String getRemark() {
		return remark;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	@Transient
	public List<String> getPermissionsName() {
		List<String> list = new ArrayList<String>();
		for (PermissionEntity per : getPermissions()) {
			System.out.println("当前权限:" + per.getPermissionname());
			list.add(per.getPermissionname());
		}
		return list;
	}

	public String getRolename() {
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "t_roles_permissions")
	public Set<PermissionEntity> getPermissions() {
		return permissions;
	}

	public void setPermissions(Set<PermissionEntity> permissions) {
		this.permissions = permissions;
	}

	@ManyToMany(mappedBy = "roles", fetch = FetchType.EAGER)
	public Set<AdminEntity> getAdmins() {
		return admins;
	}

	public void setAdmins(Set<AdminEntity> admins) {
		this.admins = admins;
	}

	@PrePersist
	public void prePersist() {
		setEnabled(true);
	}

}
